package com.offlinerssreader.model;

import java.sql.ResultSet;
import java.sql.Statement;

public class RssItem extends GenericModel {
	
	/*
	 * CREATE  TABLE `rssItems` (
	 *   `id` INT NOT NULL AUTO_INCREMENT,
	 *   `rssFeed` INT NOT NULL ,
	 *   `cache` INT NULL ,
	 *   `content` BLOB NULL ,
	 *   PRIMARY KEY (`id`) );
	 */

	private int id;
	public int rssFeed;
	public int cache;
	public String content;
	
	public static final String TABLE_NAME = "rssItems";
	public static final String COL_NAME_ID = "id";
	public static final String COL_NAME_RSS_FEED = "rssFeed";
	public static final String COL_NAME_CACHE = "cache";
	public static final String COL_NAME_CONTENT = "content";

	public RssItem() {
		super();
		this.rssFeed = 0;
		this.cache = 0;
		this.content = "";
	}
	
	public RssItem(int id) throws Exception {
		super();
		load(id);
	}
	
	public RssItem(int rssFeed, int cache, String content) {
		this.rssFeed = rssFeed;
		this.cache = cache;
		this.content = content;
	}

	@Override
	public boolean save() throws Exception {
		String query;
		Statement statement;
		if (this.id == 0)
			throw new Exception("Trying to update without id");
		
		query = "UPDATE " +  RssItem.TABLE_NAME + " SET VALUES " +
				RssItem.COL_NAME_RSS_FEED + "='" + this.rssFeed + "', " +
				RssItem.COL_NAME_CACHE + "='" + this.cache + "', " +
				RssItem.COL_NAME_CONTENT + "='" + this.content + "' " +
				" WHERE " + RssItem.COL_NAME_ID + " = '" + this.id + "'";
		
		if (this.conn.isClosed()) {
			this.conn = DBConnections.getConnection();
			if (this.conn == null) {
				return false;
			}
		}

		statement = this.conn.createStatement();
		if (statement.executeUpdate(query) < 1) {
			return false;
		}
		return true;
	}

	@Override
	public boolean delete() throws Exception {
		String query;
		Statement statement;
		if (this.id == 0)
			throw new Exception("Trying to delete without id");
		
		query = "DELETE FROM " +  RssItem.TABLE_NAME + " WHERE " + RssItem.COL_NAME_ID + " = '" + this.id + "'";
		
		if (this.conn.isClosed()) {
			this.conn = DBConnections.getConnection();
			if (this.conn == null) {
				return false;
			}
		}

		statement = this.conn.createStatement();
		if (statement.executeUpdate(query) < 1) {
			return false;
		}
		return true;
	}

	@Override
	public boolean load(Object key) throws Exception {
		this.id = (Integer)key;
		String query;
		Statement statement;
		ResultSet result;
		
		if (this.id == 0)
			throw new Exception("Trying to delete without id");
		
		query = "SELECT * FROM " +  RssItem.TABLE_NAME + " WHERE " + RssItem.COL_NAME_ID + " = '" + id + "'";
		
		if (this.conn.isClosed()) {
			this.conn = DBConnections.getConnection();
			if (this.conn == null) {
				return false;
			}
		}

		statement = this.conn.createStatement();
		result = statement.executeQuery(query);
		if (!result.next()) {
			result.close();
			return false;
		}
		
		this.cache = result.getInt(RssItem.COL_NAME_CACHE);
		this.content = result.getString(RssItem.COL_NAME_CONTENT);
		this.rssFeed = result.getInt(RssItem.COL_NAME_RSS_FEED);
		result.close();
		return true;
	}

	@Override
	public boolean create() throws Exception {
		String query;
		Statement statement;
		
		query = "INSERT INTO " +  RssItem.TABLE_NAME + " ("
				+ "`" + RssItem.COL_NAME_RSS_FEED + "`, "
				+ "`" + RssItem.COL_NAME_CACHE + "`, "
				+ "`" + RssItem.COL_NAME_CONTENT + "`"
				+ " )VALUES ("
				+ "'" + this.rssFeed + "', "
				+ "'" + this.cache + "', "
				+ "'" + this.content + "')";
		
		if (this.conn.isClosed()) {
			this.conn = DBConnections.getConnection();
			if (this.conn == null) {
				return false;
			}
		}
		
		statement = this.conn.createStatement();
		if (statement.executeUpdate(query) < 1) {
			return false;
		}
		return true;
	}
	
	public int getId() {
		return this.id;
	}

	public static String getCreateTable() {
		return "CREATE TABLE IF NOT EXISTS `" + TABLE_NAME + "` ("
				+ "`" + COL_NAME_ID + "` INT NOT NULL AUTO_INCREMENT ,"
				+ "`" + COL_NAME_RSS_FEED + "` INT NOT NULL ,"
				+ "`" + COL_NAME_CACHE + "` INT NULL ,"
				+ "`" + COL_NAME_CONTENT + "` BLOB NULL ,"
				+ "PRIMARY KEY (`" + COL_NAME_ID + "`) );";
	}
	
	public boolean isNew() throws Exception {
		String query;
		Statement statement;
		ResultSet result;

		query = "SELECT " + RssItem.COL_NAME_ID + " FROM " +  RssItem.TABLE_NAME + " WHERE `" +
				RssItem.COL_NAME_CONTENT + "`='" + this.content + "' AND `" +
				RssItem.COL_NAME_RSS_FEED + "`='" + this.rssFeed + "'";
		
		System.out.println(query);
		if (this.conn.isClosed()) {
			this.conn = DBConnections.getConnection();
			if (this.conn == null) {
				return true;
			}
		}
		
		statement = this.conn.createStatement();
		result = statement.executeQuery(query);
		if (!result.next()) {
			result.close();
			return true;
		}
		return false;
	}
	
	public boolean toggleCache() throws Exception {
		if (this.cache == 0) {
			this.cache = 1;
		} else {
			this.cache = 0;
		}
		
		return save();
	}

	public boolean doesBelondsTo(int user) throws Exception {
		if (this.rssFeed == 0) {
			throw new Exception("RssItem not yet loaded");
		}
		RssFeed feed = new RssFeed(this.rssFeed);
		return feed.doesBlongsTo(user);
	}
}
